package cn.liyicun.cp.cpweb.service;

import cn.liyicun.cp.cpweb.entity.UserMoney;
import cn.liyicun.cp.cpweb.entity.UserMoneyExample;
import cn.liyicun.cp.cpweb.mapper.UserMoneyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.UUID;

@Service
public class UserMoneyService {

	@Autowired
	UserMoneyMapper userMoneyMapper;

	public void initMoney(Long uid) {

		UserMoney record = new UserMoney();
		record.setUid(uid);
		record.setMoney(BigDecimal.valueOf(0L));
		record.setLocking(uuid());
		int i = userMoneyMapper.insertSelective(record);
	}

	private String uuid() {
		return UUID.randomUUID().toString();
	}

	public UserMoney getMoneyInfo(Long uid){
		return userMoneyMapper.selectByPrimaryKey(uid);
	}


	public int updateMoney(Long uid,String oldLooking,String newLooking,BigDecimal money){
		UserMoney record=new UserMoney();
		record.setMoney(money);
		record.setLocking(newLooking);

		UserMoneyExample example=new UserMoneyExample();
		example.createCriteria().andUidEqualTo(uid).andLockingEqualTo(oldLooking);
		return userMoneyMapper.updateByExampleSelective(record,example);
	}



}
